<?php


function getParamsList($paramName) {

        if(!isset($_REQUEST[$paramName]) )
                return array();

        $query  = explode('&', $_SERVER['QUERY_STRING']);
        $params = array();

        foreach( $query as $param ) {
                list($name, $value) = explode('=', $param);
                if (urldecode($name) == $paramName)
                        $params[] = urldecode($value);
        }

        return $params;
}



function processSearchColumns($tableName, $query) {

                ## if search parameter are passed then process them
                $searchColumnsList = getParamsList("searchColumn");
                $searchValuesList = getParamsList("searchValue");
                $valuesCount = count($searchValuesList);

                $search = "";
                foreach ($searchColumnsList as $searchColumn) {

                        $columnPosition = array_search($searchColumn, $searchColumnsList);
                        if ($columnPosition <=  $valuesCount-1) {
                                $value = $searchValuesList[$columnPosition];
                                if (strpos($searchColumn, ".") == false) {
                                        $searchColumn = $tableName.".".$searchColumn;
                                }

                        	$operator = " like ";
                                $value = str_replace('"',"",$value);
                                if (strpos($value, "?") !== false || strpos($value, "*") !== false) {
                                        $operator = " like ";
                                        $value = str_replace("?", "_", $value);
                                        $value = str_replace("*", "%", $value);
                        	}
				$value = "%".$value."%";
                        	if ($search == "")
                        		$search = " ".$searchColumn.$operator." '".$value."' ";
                        	else
                                	$search .= " OR ".$searchColumn.$operator." '".$value."' ";

                	}

                }

                if ($query != "" && $search != "")
                        $query .= " AND ( ".$search." )";
                else if ($search != "")
                        $query = "( ".$search." )";

error_log($query);

                return $query;


}


function processQueryParameters($tableName, $includeDeleted, $recordCountOnly) {

                $query = "";
		$quote = '"';

                #if ( $recordCountOnly != "Y" ) {
#
#                        if (isset($_REQUEST["lastModifiedOn"])) {
#                                $query .= " ".$tableName.".date_modified >= ";
#                                $query .= $quote.$_REQUEST["lastModifiedOn"].$quote
#                        }
#                }

#                if (!$includeDeleted)
#                        if ($query == "")
#                                $query = $tableName.".deleted=0";
#                        else
#                                $query .= " AND ".$tableName.".deleted=0";
#
                $query = processSearchColumns($tableName, $query);

                if ($query != "")
                        $query = " WHERE ".$query;

                return $query;

}


function processSortParameters() {

                $sortColumnsList = getParamsList("sortByColumn");
                #if (count($sortColumnsList) == 0)
                #        return " ORDER BY ".$tableName.".date_modified desc";

                $columnsOrderList = getParamsList("sortByOrder");
                $columnsOrderCount = count($columnsOrderList);

                $orderBy = "";
                foreach ($sortColumnsList as $sortColumn) {

                                if ($orderBy == "")
                                        $orderBy = "".$sortColumn;
                                else
                                        $orderBy .= ", ".$sortColumn;
                                $columnIndex = array_search($sortColumn, $sortColumnsList);
                                if ($columnIndex <= $columnsOrderCount-1) {
                                        $order = $columnsOrderList[$columnIndex];
                                        $order = (strcasecmp($order, "DESC") == 0) ? "DESC" : "ASC";
                                } else {
                                        $order = "ASC";
                                }

                                $orderBy .= " ".$order;
                }

                if ($orderBy != "")
                        $orderBy = " ORDER BY ".$orderBy;

                return $orderBy;
        }


?>
